#!/bin/sh

source scripts/common.sh

####################################################################################################

PNG_VERSION=1.2.43
PNG_BASENAME="libpng"
PNG_ARCHIVE="${PNG_BASENAME}-${PNG_VERSION}.tar.gz"
PNG_URL="http://prdownloads.sourceforge.net/libpng/${PNG_ARCHIVE}"
PNG_DIR="${PNG_BASENAME}-${PNG_VERSION}"
PNG_PREFIX="${THIRD_PARTY_PREFIX}/${PNG_BASENAME}"
PNG_LIB=libpng.a

####################################################################################################

function build_png()
{

target=$1

mkdir -p ${THIRD_PARTY_BUILD} || exit 1
pushd ${THIRD_PARTY_BUILD} || exit 1

report ""
report "Removing old build from '${PNG_DIR}'..."
rm -fr $PNG_DIR
rm -fr $PNG_PREFIX/${target}

report "Preparing ${target} '${PNG_BASENAME}'..."
fetch $PNG_URL $PNG_ARCHIVE

report "Configuring ${target} '${PNG_BASENAME}'..."
report ""

pushd ${PNG_DIR} || exit 1
mkdir -p ${target} || exit 1

./configure \
		CXXFLAGS="${SYSROOT} -arch ${target}" \
		CFLAGS="${SYSROOT} -arch ${target}" \
		LDFLAGS="-Wl,-syslibroot,${DEVSDK}" \
		--prefix=${PNG_PREFIX}/${target} \
        --target=${target}-apple-darwin \
        --disable-shared \
        --enable-static \
        --quiet \
        --silent

if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

report "Building ${target} '${PNG_BASENAME}'..."
nice make -j4 || exit 1
nice make install || exit 1

report "Built ${target} '${PNG_BASENAME}'"
report ""

popd
popd

}

####################################################################################################

build_for_snow_leopard
for target in 'i686' 'x86_64' 'ppc'
do
	build_png $target
done

create_universal_lib ${PNG_PREFIX} ${PNG_LIB}
create_intel_lib ${PNG_PREFIX} ${PNG_LIB}
create_powerpc_lib ${PNG_PREFIX} ${PNG_LIB}

####################################################################################################

